package top.longmarch.flow.component;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.longmarch.flow.dto.LiteFlowDTO;
import top.longmarch.flow.entity.LiteflowChain;
import top.longmarch.flow.exception.BusinessException;
import top.longmarch.flow.slot.LiteflowChainContext;

import java.util.List;

@Slf4j
@Component("checkFlowChainCodeCmp")
public class CheckFlowChainCodeCmp extends NodeComponent {

    @Override
    public void process() throws Exception {
        LiteflowChainContext context = this.getContextBean(LiteflowChainContext.class);
        List<LiteflowChain> liteflowChainList = context.getLiteflowChainList();
        if (CollectionUtils.isNotEmpty(liteflowChainList)) {
            throw new BusinessException(5000, "流程编码已存在");
        }
    }

    @Override
    public boolean isAccess() {
        LiteFlowDTO req = this.getRequestData();
        return req == null || req.getId() == null;
    }

}
